# study-code
# depth-first search 2
#
# The Number of Island
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0067
#
# start 2011/10/10 02:30
# end   2011/10/10 03:35
DATA_INPUT = <<DATA
111100001111
111000001111
110000001111
100000001111
000100010000
000000111000
000001111100
100011111110
110001111100
111000111000
111100010000
000000000000

010001111100
110010000010
010010000001
010000000001
010000000110
010000111000
010000000100
010000000010
010000000001
010010000001
010010000010
111001111100

000000000000
111111111111
100010100001
100010100001
100010100001
100010100001
100100100101
101000011101
100000000001
100000000001
111111111111
100000000001

DATA

def parse_input( str )
    input = []
    map = []

    str.split("\n").each{|data_set|
        if(data_set.length ==0)
            input.push( map )
            map = []
        else
            map.push data_set
        end
    }
    if(map.length > 0)
        input.push map
    end
    return input
end


class Node
    attr_accessor :child

    def ==(other)
        other.object_id == self.object_id
    end

end
class Searcher 
    def initialize
        @checked_map = Array.new( 12 ).map!{|v| [nil] * 12 }
        @root = []
    end

    def search(map)
        for j in (0 ... 12) do
            for i in (0 ... 12) do
                land = ((map[j][i] - '0'[0]) == 1)
                if land
                    #puts "#{i} , #{j} : " + @checked_map.inspect
                    left = ( i > 0 ) ?  @checked_map[j][i-1] : nil
                    top  = ( j > 0 ) ?  @checked_map[j-1][i] : nil
                    node = nil

                    if ( left )
                        node = left
                        @checked_map[j][i] = left
                    end

                    if( top )
                        if( left && top )
                            if(left != top)
                                left.child = top
                                @root.delete(top)
                            end
                        elsif top
                            node = top
                            @checked_map[j][i] = top
                        end
                    end
                    if( node == nil )
                        node = Node.new
                        @root.push node
                        @checked_map[j][i] = node
                    end
                end

            end
        end
        return @root
    end
end

set = parse_input DATA_INPUT
p set
set.each{|v|
    s = Searcher.new
    puts s.search( v ).length
}


